<?php

class ClientManager extends ORMManager implements SessionObjectInterface{
	
	const  AUTH_USER_EXISTS 	 = 1;
	const  AUTH_REGISTER_SUCCESS = 2;
	const  AUTH_ACCESS_DENIED	 = 3;
	const  AUTH_USER_NOT_FOUND   = 4;
	const  AUTH_ACTICODE_INVALID = 5;
	const  AUTH_ACTICODE_VALID	 = 6;
	const  AUTH_LAST_ADMIN_ACCOUNT = 7;
	const  AUTH_PERIOD_INVALID 	 = 8;
	const  AUTH_ADMIN_BLOCK	     = 9;
	const  AUTH_FAKE_USER	     = 10;
	
	private $expiredDays = 365;
	
	private $cookies_name = 'PSP_ID';
	
	 /**
     * @var Passport_ClientsDBO     the clients DBO
     */
	
	protected $client_object;
	
	protected $auth_status = null;
	
	public function __construct(){
		parent::__construct();
		$this->client_object = new Passport_ClientsDBO();
	}
	
	public function getClientId(){
		return $this->client_object->clientID;
	}
	
	public function loginById($client_id){
		if ($this->isAuthed()){
    		return self::AUTH_REGISTER_SUCCESS;
    	}else{
			$object_new = $this->client_object->getTable()->createQuery()->addWhere('clientID='.$client_id)->fetchOne();
    		$this->client_object = $object_new;
			if ($this->isBlocked())
			{
				return self::AUTH_PERIOD_INVALID;
			}else{
				if ($this->isBlockedByAdmin()){
					$this->auth_status = self::AUTH_ADMIN_BLOCK;
					return self::AUTH_ADMIN_BLOCK;
				}else{
					$this->auth_status = self::AUTH_REGISTER_SUCCESS; 
					return self::AUTH_REGISTER_SUCCESS;
				}	
			}
    	}
	}
	
	public function login($email,$password,$option = array())
    {
    	if ($this->isAuthed()){
    		return self::AUTH_REGISTER_SUCCESS;
    	}else{

    		$object_new = $this->client_object->getTable()->createQuery()->addWhere("email='".strtolower($email)."'")->fetchOne();
    		
    		if (is_object($object_new))
    		{
				$this->client_object = $object_new;
				if ($this->isBlocked())
				{
					return self::AUTH_PERIOD_INVALID;
				}else{
					if ($this->isBlockedByAdmin()){
						$this->auth_status = self::AUTH_ADMIN_BLOCK;
						return self::AUTH_ADMIN_BLOCK;
					}else{
						if ($this->client_object->password == $this->encriptPass($password)){
							if ($option['remember']){
								Ria_Client::getInstance()->Session->addKey();
							}
							$this->auth_status = self::AUTH_REGISTER_SUCCESS; 
							return self::AUTH_REGISTER_SUCCESS;
						}else{
							$this->auth_status = self::AUTH_FAKE_USER;
							return self::AUTH_FAKE_USER;
						}
					}	
				}
    		}else{
    			$this->auth_status = self::AUTH_USER_NOT_FOUND;
   				return self::AUTH_USER_NOT_FOUND;
    		}
    	}
    }
    
    private function encriptPass($password){
    	return md5($password);
    }
    
	public function isBlockedByAdmin(){
		return false;
	}
	
	public function isAuthed(){
		 if ($this->auth_status == self::AUTH_REGISTER_SUCCESS){
		 	return true;
		 }else{
		 	return false;
		 }
	}
	
	public function isBlocked(){
		if (!$this->isAuthed()){
			return false;
		}
		
		if ($this->client_object->status){
			return false;
		}
		$time = $this->client_object->date;
		if (($time+60*60*24)<time()){
			return true;
		}
		return false;
	}
	
  	public function getObjectState(){
  		return array('auth_status' => $this->auth_status,'client_object'=>$this->client_object->serialize());
  	}
  
 	public function setObjectState($objectState){
 		$this->auth_status = $objectState['auth_status']; 
  		$this->client_object->unserialize($objectState['client_object']);
  	}
  	
	public function logout() {
		$this->auth_status = self::AUTH_USER_EXISTS;
	 	Ria_Client::getInstance()->Session->sessionClose();
	}
	
	public function register($data){
		
		$flag = $this->login($data['email'],$data['password']);
		if ($flag == self::AUTH_REGISTER_SUCCESS){
			return self::AUTH_USER_EXISTS;
		}elseif ($flag == self::AUTH_FAKE_USER){

			$user_fake = new Passport_ClientsDBO();
			$user_fake->fromArray($data);
    		$user_fake->email = $this->client_object->clientID . "@fake_auto.ria.ua";
    		$user_fake->save();
    		
			
    		
    		$fake = new Passport_ClientsFakeDBO();
    		$fake->client_id =  $this->client_object->clientID;
    		$fake->project_id = 1;
    		$fake->fake_id = $user_fake->clientID;
    		$fake->create_date = time();
    		$fake->remind_date = '1999-01-01 00:00:00';
    		$fake->fake_password = $data['password'];
    		$fake->save();
    		$this->client_object = $user_fake;
    		return self::AUTH_FAKE_USER;
		}
		
		$this->client_object->fromArray($data);
   		$this->client_object->save();
		return self::AUTH_REGISTER_SUCCESS;
		
	}
	
	public function checkFakeUser($fake_id){
		$fake_user = $this->getQuery()->from('Passport_ClientsFakeDBO')->where('fake_id='.$fake_id)->fetchOne();
		if (is_object($fake_user)){
			$fake_manager = new FakeManager();
			$flag = $fake_manager->moveData($fake_user->fake_id,$fake_manager->client_id);
			if ($flag){
				$this->getQuery()->delete()->from('Passport_ClientsFakeDBO')->where('fake_id='.$fake_id)->execute();
				$this->getQuery()->delete()->from('Passport_ClientsDBO')->where('clientID='.$fake_id)->execute();
				return $fake_manager->client_id;
			}
		}
		return false;
	}
}

?>